package CreationModel.PrototypeModel.DeepAndShallowClone.DeepCloneTwo;

import com.google.gson.Gson;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * @author 王蕾 [wanglei_dev@cyou-inc.com]
 * @date 2017-12-06 17:49:00
 */
public class Swallow implements Cloneable, Serializable{

    private static final long serialVersionUID = -2152309743441152834L;
    private String name;
    private Wing leftWing;
    private Wing rightWing;

    public Swallow(String name, Wing leftWing, Wing rightWing) {
        this.name = name;
        this.leftWing = leftWing;
        this.rightWing = rightWing;
    }



    public Object deepClone() throws IOException, ClassNotFoundException{
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(this);

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        return ois.readObject();
    }

    public Swallow deepClone2(){
        Gson gson = new Gson();
        String json = gson.toJson(this);
        return gson.fromJson(json, this.getClass());
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Wing getLeftWing() {
        return this.leftWing;
    }

    public void setLeftWing(Wing leftWing) {
        this.leftWing = leftWing;
    }

    public Wing getRightWing() {
        return this.rightWing;
    }

    public void setRightWing(Wing rightWing) {
        this.rightWing = rightWing;
    }
}
